package com.haredot.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haredot.dto.ModifyUserDTO;
import com.haredot.holder.TokenContextHolder;
import com.haredot.oss.properties.OssProperties;
import com.haredot.service.UserService;
import com.haredot.vo.R;
import com.haredot.vo.UserFriendsVo;
import com.haredot.vo.UserScoreVo;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@EnableConfigurationProperties(OssProperties.class)
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private OSS ossClient ;

    @Resource
    private OssProperties ossProperties ;

    /**
     * 设置用户 关注/取消关注
     *
     * @return
     */
    @PutMapping("/attention")
    public R<?> attentionUser(@RequestParam("to") Long toUserId) {

        // 调用业务层处理业务
        return userService.attentionUser(toUserId);
    }

    /**
     * 根据条件修改用户
     * @param avatar
     * @param nickName
     * @param sex
     * @param oldEmail
     * @param newEmail
     * @return
     */
    @PutMapping("/modify")
    public R<?> modifyUser(@RequestParam(value = "avatar", required = false) MultipartFile avatar,
                           @RequestParam(value = "nickName", required = false) String nickName,
                           @RequestParam(value = "sex", required = false) String sex,
                           @RequestParam(value = "oldEmail", required = false) String oldEmail,
                           @RequestParam(value = "newEmail", required = false) String newEmail,
                           @RequestParam(value = "oldPassword", required = false) String oldPassword,
                           @RequestParam(value = "newPassword", required = false) String newPassword) throws Exception {

        if (avatar == null &&
                !StringUtils.hasLength(nickName) &&
                !StringUtils.hasLength(sex) &&
                !StringUtils.hasLength(oldPassword) &&
                !StringUtils.hasLength(newPassword) &&
                !StringUtils.hasLength(oldEmail) &&
                !StringUtils.hasLength(newEmail)) {

            return R.fail("丢失必传的参数").build();
        }

        ModifyUserDTO modifyUserDTO = new ModifyUserDTO() ;
        // 如果 头像 不为空，则进行文件上传，并获取 文件名
        if (avatar != null) {
            String filename = "avatar/" + UUID.randomUUID().toString() + avatar.getOriginalFilename() ;
            ossClient.putObject(new PutObjectRequest(ossProperties.getBucketName(), filename,  avatar.getInputStream()));
            modifyUserDTO.setAvatar(filename);
        }

        modifyUserDTO.setSex(sex);
        modifyUserDTO.setNickName(nickName);

        // 如果 邮箱 不正确
        if (StringUtils.hasLength(oldEmail) && !StringUtils.hasLength(newEmail) ||
            StringUtils.hasLength(newEmail) && !StringUtils.hasLength(oldEmail)) {
            return R.fail("邮箱参数丢失").build();
        }

        modifyUserDTO.setNewEmail(newEmail);
        modifyUserDTO.setOldEmail(oldEmail);

        if (StringUtils.hasLength(oldPassword) && !StringUtils.hasLength(newPassword) ||
                StringUtils.hasLength(newPassword) && !StringUtils.hasLength(oldPassword)) {
            return R.fail("密码参数丢失").build();
        }

        if (StringUtils.hasLength(newPassword) && !newPassword.matches("^.{6,20}$")) {
            return R.fail("密码长度必须在 6 ~ 20 位").build();
        }

        modifyUserDTO.setOldPassword(oldPassword);
        modifyUserDTO.setNewPassword(newPassword);

        modifyUserDTO.setId(TokenContextHolder.getCurrentUserId());

        // 准备 添加数据
        return userService.modifyUser(modifyUserDTO) ;

    }

    @GetMapping("/score/list")
    public R<Page<UserScoreVo>>  scoreList(@RequestParam(value = "page", defaultValue = "1", required = false) Long current,
                                           @RequestParam(value = "rows", required = false, defaultValue = "10") Long rows) {
        // 创建一个 分页对象
        Page<UserScoreVo> page = new Page<>(current, rows) ;
        // 查询数据、并进行分页操作
        return userService.queryUserScores(page) ;
    }

    /**
     * 查询 用户积分数、粉丝数、关注数、积分来源
     * @return
     */
    @GetMapping("/score/shape")
    public R<Map<String, Object>>  scoreShapeDetails() {
        // 查询数据
        return userService.queryScoreShapeDetails() ;
    }

    @GetMapping("/friends")
    public R<UserFriendsVo> queryFriends() {

        return userService.queryFriendList();
    }

    @GetMapping("/friend/{uid}")
    public R<UserFriendsVo> friendDetail(@PathVariable("uid") Long uid) {
        return userService.queryFriendDetail(uid) ;
    }

    @PutMapping("/check/friend/{uid}")
    public R<?> checkFriend(@PathVariable("uid") Long uid) {

        return userService.checkFriend(uid);
    }

}
